package test.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 *
 * @ project name : ecny-wallet-base
 * @ Author: XuLeHuang
 * @ Date: 2022/7/26 16:36
 * @ Description:
 */
@Component
public class FileHandelUtil {
    @Value("${rootPath}")
    private  String rootPath;

    @Value("${suffix}")
    private  String suffix;

    public  String getStringFromFile(String str) {
        //获取String字符串
        //转成json
        JSONObject jsonObject = JSON.parseObject(str);
        //获取文件标识符
        Map head = (Map)jsonObject.get("reqHeader");
        String fileFlag = (String) head.get("serviceId");
        //根据文件标识符获取文件内容
        String respString = "";
        try (FileInputStream file = new FileInputStream(rootPath + fileFlag + suffix);
             InputStreamReader reader = new InputStreamReader(file,"UTF-8");
             BufferedReader br = new BufferedReader(reader);)  {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                respString += line;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //将文件内容转成json返回
        return respString;
    }

    public  String getStringFromFileForOffice(String serviceId) {
        //根据文件标识符获取文件内容
        String respString = "";
        try (FileInputStream file = new FileInputStream(rootPath + serviceId + suffix);
             InputStreamReader reader = new InputStreamReader(file,"UTF-8");
             BufferedReader br = new BufferedReader(reader);)  {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
                respString += line;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //将文件内容转成json返回
        return respString;
    }

    /*public static boolean writeToFileByAppend(String line) {
        try (FileOutputStream file = new FileOutputStream("D:\\cas-log.txt" , true);
             OutputStreamWriter writer = new OutputStreamWriter(file,"UTF-8");
             BufferedWriter br = new BufferedWriter(writer);)  {
             br.write(line);
             br.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(Runtime.getRuntime().availableProcessors(), new ThreadFactory() {
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setName("com.alibaba.nacos.client.Worker.longPolling." + agent.getName());
                t.setDaemon(true);
                return t;
            }
        });
        executorService.scheduleWithFixedDelay(new Runnable() {
            public void run() {
                try {
                    ClientWorker.this.checkConfigInfo();
                } catch (Throwable var2) {
                    ClientWorker.LOGGER.error("[" + agent.getName() + "] [sub-check] rotate check error", var2);
                }

            }
        }, 1L, 10L, TimeUnit.MILLISECONDS);
        return true;
    }*/
}
